package com.novelastudios.utils
{
	/**
	 * 
	 * @author NovelaStudios, John Bailey ©2009
	 * 
	 */	
	public class MathUtil
	{
		/**
		 * 
		 * @param target
		 * @return 
		 * 
		 */		
		public static function getScaleBy(width:Number,height:Number):String
		{			
			var value:String = "width";
			if(height > width)
			{
				value = "height";
			}
			
			return value;
		}
		
		/**
		 * 
		 * @param width
		 * @param height
		 * @return 
		 * 
		 */		
		public static function getAspectRatio(width:Number,height:Number):Number
		{			
			var value:Number = 0;
			if(height > width){
				value = width/height;
			}else if(height < width){
				value = height/width;
			}else if(height == width){
				value = width/height;
			}
			return value;
		}
		
		/**
		 * @cite Algorithm compliments to RJ Regenold.
		 */ 
		public static function randomBetween(low:Number,high:Number,decimalPlace:Number=-1):Number
		{
			var number:Number = ( Math.random() * ( high - low ) ) + low;
			if(decimalPlace > 0)
			{
				number = MathUtil.roundDecimal(number,decimalPlace);
			}
			else if(decimalPlace==0)
			{
				number = Math.round(number);
			}
			return number;
		}
		
		/**
		 * @cite http://www.ear-fung.us/2008/04/flash-rounding-function/
		 */ 
		public static function roundDecimal(num:Number, places:Number):Number
		{
			var number:Number = int( (num) * Math.pow(10, places) ) / Math.pow(10, places);
			if(places<=0)
			{
				number = Math.round(number);
			}
			return number;
		}
		
		/**
		 * 
		 * @param value
		 * @return 
		 * 
		 */		
		public static function calculateString(value:String):Number
		{
			var operatorsArr:Array = value.match(/(\D)+/g);
			var numbersArr:Array = value.match(/(\d)+/g);
			var total:Number = 0;
			var index:Number = 0;
			for(var i:Number = 0; i < numbersArr.length; i++)
			{
				if(i == 0 && numbersArr.length != operatorsArr.length)
				{
					total = parseFloat(numbersArr[0]);
				}
				else
				{
					index = (numbersArr.length == operatorsArr.length) ? i : i-1;
					switch(operatorsArr[index])
					{
						case "+":
							total += parseFloat(numbersArr[i]);
							break;
						
						case "-":
							total -= parseFloat(numbersArr[i]);
							break;
						
						case "*":
							total *= parseFloat(numbersArr[i]);
							break;
						
						case "/":
							total /= parseFloat(numbersArr[i]);
							break;
					}
				}
			}
			return total;
		}
		
		/**
		 * Convert a degree to a radian equivalent  
		 * @param degree
		 * @return 
		 * 
		 */		
		public static function getRadians(degree:Number):Number
		{
			return degree * Math.PI/180;
		}
		
		/**
		 * As Michael Baczynski explains (http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/)
		 *  using the bitwise of & is much faster than % technique/algorithm
		 * 
		 */		
		public static function isOdd(value:Number):Boolean
		{
			return ((value & 2) == 0);
			
		}
		
		/**
		 * As Michael Baczynski explains (http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/)
		 *  using the bitwise of & is much faster than % technique/algorithm
		 * 
		 */		
		public static function isEven(value:Number):Boolean
		{
			return ((value & 1) == 0);
		}
	}
}